
#include <stdio.h>


#include <linux/rtc.h>


#include <sys/types.h>
#include <sys/socket.h>
#include <sys/ioctl.h>
#include <sys/wait.h>
#include <sys/time.h>

#include <netinet/in.h>
#include <arpa/inet.h>
#include <unistd.h>
#include <dirent.h>
#include <signal.h>
#include <string.h>
#include <stdlib.h>
#include <stdio.h>
#include <errno.h>
#include <time.h>
#include <getopt.h>
#include <assert.h>
#include <math.h>

#include <fcntl.h>
#include <ctype.h>

#include <limits.h>

#include <netinet/in_systm.h>
#include <netinet/ip.h>
#include <netinet/tcp.h>


#include <aircrack-ng/defs.h>
#include <aircrack-ng/support/pcap_local.h>
#include <aircrack-ng/osdep/osdep.h>
#include "aircrack-ng/support/common.h"
#include "aircrack-ng/support/communications.h"
#include <aircrack-ng/third-party/ieee80211.h>



#include <unistd.h>
#include <string.h>
#include <stdlib.h>
#include <stdio.h>
#include <netpacket/packet.h>
#include <linux/if_ether.h>
#include <sys/ioctl.h>
#include <arpa/inet.h>
#include <sys/time.h>
#include <net/if.h>

typedef signed char int8;
typedef unsigned char uint8;
typedef signed short int16;
typedef unsigned short uint16;
typedef signed int int32;
typedef unsigned int uint32;
typedef signed long long int64;
typedef unsigned long long uint64;

struct ap
{
    uint8 bssid[6];
    uint16 seq_id;
    uint8 essid_len;
    char essid[320];
};

struct wif  *_wi_out;

void init_ap(struct ap* p_ap,uint8* p_bssid,char* p_essid)
{
    memcpy(p_ap->bssid,p_bssid,6);
    p_ap->seq_id=0;
    uint32 t_len=strlen(p_essid);
    if(t_len>32)
        t_len=32;
    p_ap->essid_len=t_len;
//    uint8 p_buffer33[13]={0x01,0x08,0x82,0x84,0x8b,0x96,0x12,0x24,0x48,0x6c,0x03,0x01,0x01};

    //memcpy(p_essid,p_buffer33,13);
    memcpy(p_ap->essid,p_essid,t_len);
  //  memcpy(p_ap->essid+t_len,p_buffer33,13);
}

void lhfpp(uint8* buffer,int len)
{
	for(int lhf_len=0;lhf_len<len;lhf_len++)
        {
                printf(" 0x%x",buffer[lhf_len]);
        }

	printf("\n\n");

}

uint16 create_beacon_frame(uint8* p_buffer,struct ap* p_ap)
{

    uint8 p_buffer1[20]={0x80,0x00,0x00,0x00,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF};
    memcpy(p_buffer,p_buffer1,10);


   // lhfpp(p_buffer,10);

    memcpy(p_buffer+10,p_ap->bssid,6);
    memcpy(p_buffer+16,p_ap->bssid,6);
    p_buffer[22]=(uint8)(p_ap->seq_id&0xFF);
    p_buffer[23]=(uint8)((p_ap->seq_id>>8)&0xFF);
    p_ap->seq_id+=0x10;
    struct timeval t_time;
    gettimeofday(&t_time,0);
    uint64 t_timestamp=((uint64)t_time.tv_sec)*1000+t_time.tv_usec;
    uint8 t_i;
    for(t_i=0;t_i<8;t_i++)
         p_buffer[24+t_i]=(uint8)((t_timestamp>>(t_i<<3))&0xFF);
    //占用时间 0x64 102400um
    //uint8 p_buffer2[20]={0x64,0x00,0x01,0x00};
    uint8 p_buffer2[20]={0xff,0x00,0x01,0x00};
    memcpy(p_buffer+32,p_buffer2,4);
    p_buffer[36]=0;
    p_buffer[37]=p_ap->essid_len;
    memcpy(p_buffer+38,p_ap->essid,p_ap->essid_len);
    return 38+p_ap->essid_len;
}

int32 create_raw_socket(char* p_iface)
{
    /* new raw socket */
    //int32 t_socket=socket(PF_PACKET,SOCK_RAW,htons(ETH_P_ALL));
    int32 t_socket=socket(PF_PACKET,SOCK_RAW,htons(ETH_P_ALL));
    if(t_socket<0)
    {
        perror("<create_raw_socket> socket(PF_PACKET,SOCK_RAW,htons(ETH_P_ALL)) failed!");
        return -1;
    }
    /* get the index of the interface */
    struct ifreq t_ifr;
    memset(&t_ifr,0,sizeof(t_ifr));
    strncpy(t_ifr.ifr_name,p_iface,sizeof(t_ifr.ifr_name)-1);
    if(ioctl(t_socket,SIOCGIFINDEX,&t_ifr)<0)
    {
        perror("<create_raw_socket> ioctl(SIOCGIFINDEX) failed!");
        return -1;
    }
    /* bind the raw socket to the interface */
    struct sockaddr_ll t_sll;
    memset(&t_sll,0,sizeof(t_sll));
    t_sll.sll_family=AF_PACKET;
    t_sll.sll_ifindex=t_ifr.ifr_ifindex;
    t_sll.sll_protocol=htons(ETH_P_ALL);
    if(bind(t_socket,(struct sockaddr*)&t_sll,sizeof(t_sll))<0)
    {
        perror("<create_raw_socket> bind(ETH_P_ALL) failed!");
        return -1;
    }
    /* open promisc */
    struct packet_mreq t_mr;
    memset(&t_mr,0,sizeof(t_mr));
    t_mr.mr_ifindex=t_sll.sll_ifindex;
    t_mr.mr_type=PACKET_MR_PROMISC;
    if(setsockopt(t_socket,SOL_PACKET,PACKET_ADD_MEMBERSHIP,&t_mr,sizeof(t_mr))<0)
    {
        perror("<create_raw_socket> setsockopt(PACKET_MR_PROMISC) failed!");
        return -1;
    }
    return t_socket;
}

int32 send_80211_frame(int32 p_socket,uint8* p_buffer,uint32 p_size)
{
    uint8 t_buffer[4096];
    //uint8 t_radiotap1[13]={0x00,0x00,0x0c,0x00,0x04,0x80,0x02,0x00,0x02,0x00,0x00,0x00,0x00};
    //uint8 t_radiotap2[18]={0x00,0x00,0x12,0x00,0x2e,0x48,0x00,0x00,0x00,0x16,0x6c,0x09,0xa0,0x00,0xda,0x00,0x00,0x00};
   // memcpy(t_buffer,t_radiotap1,18);
   // memcpy(t_buffer+18,p_buffer,p_size);
    memcpy(t_buffer,p_buffer,p_size);
   // p_size+=18;
//-----------------------------

    //uint8 p_buffer33[13]={0x01,0x08,0x82,0x84,0x8b,0x96,0x12,0x24,0x48,0x6c,0x03,0x01,0x01};
    //memcpy(t_buffer+p_size,p_buffer33,13);
    // p_size+=13;
     send_packet(_wi_out, t_buffer, p_size, kRewriteSequenceNumber);
    //int32 t_size=write(p_socket,t_buffer,p_size);
//    lhfpp(t_buffer,p_size);
//    可以在t_buffer后加需要的数据
    //memcpy(t_buffer+p_size,需要的数据数组,长度);
    if(p_size<0)
    {
        perror("<send_80211_frame> write() failed!");
        return -1;
    }
    return p_size;
}


int32 main(int argc, char * argv[])
{
    struct ap t_ap1,t_ap2;
    char  name[32],name1[32];
    //uint8 * name="lhf66";
     _wi_out = wi_open(argv[1]);
    uint8 t_ap11[10]={0xEC,0x17,0x2F,0x2D,0xB6,0x0};
    uint8 t_ap22[10]={0xF7,0x16,0x2F,0x2D,0xB6,0x0};
    //init_ap(&t_ap1,t_ap11,"zjs ap 1");
   // init_ap(&t_ap2,t_ap22,"lhf666");
    uint8 t_buffer[1024];
    uint8 t_buffer1[1024];
    int32 t_socket=create_raw_socket("wlxe84e068a804c");
    //memcpy(name,"aEBF_QC",9);
    memcpy(name,"AAAA_QC",9);
    memcpy(name1,"AAAA_QB",9);

    while(1)
    {

	for(int count_lhf=0;count_lhf<254;count_lhf++)
	{
		init_ap(&t_ap2,t_ap22,name);
		init_ap(&t_ap1,t_ap11,name1);
	

        int t_len=create_beacon_frame(t_buffer,&t_ap2);
        int t_len1=create_beacon_frame(t_buffer1,&t_ap1);
  //      printf("\n%d\n",send_80211_frame(t_socket,t_buffer,t_len));
	send_80211_frame(t_socket,t_buffer,t_len);
	send_80211_frame(t_socket,t_buffer1,t_len1);
//	usleep(1);

//	printf("%d\n",count_lhf);


	//printf("%c\n",t_ap22[5],name[5]);


	t_ap22[5]++;
	t_ap11[5]++;
	name[7]++;
	name1[7]++;
	if(t_ap22[5]>100)
	{
		t_ap22[5]=0;
		t_ap11[5]=0;
	}

	if(name[7]>100)
        {
                name[7]=1;
                name1[7]=1;
        }


	}
    }
    return 0;
}
